package edu.nf.supermarket.web.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.consts.PayStatusEnum;
import edu.nf.supermarket.entity.Coupon;
import edu.nf.supermarket.entity.Orders;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.CouponService;
import edu.nf.supermarket.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yjj
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CouponService couponService;

    /**
     * 根据订单id获取 订单信息
     */
    @RequestMapping("/findOrderById")
    public ResultVO<Orders> findOrderById(@RequestParam("orderId") String orderId) {
        Orders orders = orderService.findOrderByOrderID(orderId);
        return success(orders);
    }

    /**
     * 根据用户id获取订单信息
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */

    @RequestMapping("/findCommodityByUserId")
    public PageInfo<Orders> findCommodityByUserId(@RequestParam("userId") Integer userId,
                                                  @RequestParam(value = "status", required = false, defaultValue = "0") Byte status,
                                                  @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        PageInfo<Orders> pageInfo = orderService.findOrderByUserId(userId, status, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 返回符合使用条件的优惠卷
     */
    @GetMapping("/matchConditionCoupon")
    public ResultVO<List<Coupon>> matchConditionCoupon(@RequestParam("userId") Integer userId, @RequestParam("amount") BigDecimal amount) {
        List<Coupon> couponByUserId = couponService.findCouponByUserId(userId);
        List<Coupon> couponList = couponByUserId.stream().filter(coupon -> coupon.getCouponCondition().compareTo(amount) <= 0 &&
                coupon.getCouponStatus() == 0
        ).collect(Collectors.toList());
        return success(couponList);
    }

    /**
     * 根据订单号或商品名模糊查询订单
     */
    @GetMapping("/searchOrdersByKeyword")
    public ResultVO<List<Orders>> searchOrdersByKeyword(@RequestParam("keyword") String keyword, @RequestParam("userId") Integer userId) {
        List<Orders> orders = orderService.searchOrdersByKeyword(keyword, userId);
        return success(orders);
    }

    /**
     * 根据时间查询订单
     */
    @GetMapping("/findOrdersByDateRange")
    public ResultVO<List<Orders>> findOrdersByDateRange(@RequestParam("startTime") String startTime,
                                                        @RequestParam("endTime") String endTime,
                                                        @RequestParam("userId") Integer userId) {

        List<Orders> orders = orderService.findOrdersByDateRange(LocalDate.parse(startTime), LocalDate.parse(endTime), userId);
        return success(orders);
    }
    /**
     * 订单收货
     * */
    @GetMapping("/receiveOrder")
    public ResultVO<String> receiveOrder(@RequestParam("orderId") String orderId){
        orderService.updateOrderStatus(orderId, (byte) 3);
        return success("收货成功");
    }
    /**
     * 删除订单
     */
    @GetMapping("/deleteOrder")
    public ResultVO<String> deleteOrder(@RequestParam("orderId") String orderId) {
        orderService.deleteOrder(orderId);
        return success("删除成功");
    }
    /**
     * 取消订单
     */
    @GetMapping("/cancelOrder")
    public ResultVO<String> cancelOrder(@RequestParam("orderId") String orderId) {
        orderService.cancelOrder(orderId);
        return success("取消成功");
    }
}
